package com.ximu.bus;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;

public class Html {

	/**
	 * get
	 * @param httpurl
	 * @return
	 */
	public static  String get(String httpurl){
		String line = "";
		String html = "";
		try {
			URL url = new URL(httpurl);
			HttpURLConnection httpconn =  (HttpURLConnection) url.openConnection();
			InputStream in = httpconn.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			while((line=br.readLine())!=null){
				html += line;
			}
		} catch (Exception e) {
			return "";
		}
		return html;
	}
	
	/**
	 * post
	 * @return
	 */
	public static  String post(String url,HashMap<String, String> param){
		String line = "";
		String html = "";
		String paramstr = "";
		try{
			URL httpURL = new URL(url);
			HttpURLConnection httpConn = (HttpURLConnection) httpURL.openConnection();
			Iterator iter = param.keySet().iterator();
			while (iter.hasNext()) { 
				String key = (String)iter.next(); 
				String value = (String)param.get(key);
				paramstr += key+"="+value+"&";
			}
			httpConn.setDoOutput(true);
			httpConn.setRequestMethod("POST");
			httpConn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Firefox/3.6.8");
			httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			httpConn.getOutputStream().write(paramstr.getBytes());
			InputStream in = httpConn.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			while((line=br.readLine())!=null){
				html += line;
			}
		}catch(Exception e){
			return "";
		}
		return html;
	}
}
